@echo off

if "%1"=="" (
	echo You have missed some parameters.
	echo Please, pass at least one parameter - function name.
	echo Press any key...
	pause> nul
) else (
	call :%1 %2 %3
)
goto :EOF



:configure
rem  Project name
call :get_name . Name
rem  Default defines (separator - space)
set Defines=EP2
rem  Default libs (separator - ;)
set Libs=std.sa;libc.a
rem  Additional libs & defines
if not "%AddonLibs%"=="" set Libs=%Libs%;%AddonLibs%
if not "%AddonDefs%"=="" set Defines=%Defines% %AddonDefs%

rem  Output directory
set BinDir=bin
rem  Sources directory
set SrcDir=src
rem  Unpostlinked libraries directory
set LibDir=lib
rem  Documentation directory
set DocDir=doc
rem  Resources directory
set ResDir=res
rem  EmuElf directory
set EmuDir=emuelf
rem  Includes directories (separator - ;)
set IncludesDirs=E:\Repository\SDK
rem  Libs directories (separator - ;)
set LibsDirs=E:\Repository\libgen
rem  Time and date detection
for /f "tokens=1-3 delims=/-:., " %%i in ('date /T') do set _date=%%k%%j%%i
for /f "tokens=1-2 delims=/-:., " %%i in ('time /T') do set _time=%%i%%j
set now=%_date:~2,6%_%_time%

rem  Compiler, linker and other utilities options
set 7z=7z.exe
set gcc=arm-eabi-gcc.exe
set ld=arm-eabi-ld.exe
set pl=postlink.exe
set C_Opts=-c -O -nostdlib -fshort-wchar -fshort-enums -fpack-struct=4 -fno-builtin -fvisibility=hidden -Wimplicit -Wstrict-prototypes -Werror
set C_Opts_M680x0=-mbig-endian -mthumb -mthumb-interwork
set Ld_Opts=-EB -nostdlib --as-needed -gc-sections -wrap memcpy -wrap vsprintf -wrap vsnprintf

cls
exit /b %errorlevel%



:build
set exitcode=0
set append=

call :configure

call :color build%2
call :title build%2

goto build_define%2

:build_definedebug
set Defines=%Defines% DEBUG
set Append=_dbg
goto build_define
:build_definetask
set Defines=%Defines% USE_TASKS
set Append=_tsk
goto build_define
:build_define

call :compile "%SrcDir%\*.c"

goto build_target%1

:build_targetelf
if not errorlevel 1 (
	call :link elf "*.o" "%Name%.pe"
	if not errorlevel 1 (
		call :postlink elf "%Name%.pe" "%BinDir%\%Name%%append%.elf"
	) else (
		set exitcode=1
		call :color error
		call :title error
		pause
	)
) else (
	set exitcode=1
	call :color error
	call :title error
	pause
)
goto build_target
:build_targetlib
if not errorlevel 1 (
	call :link lib "*.o" "%Name%.lib"
	if not errorlevel 1 (
		call :postlink lib "%Name%.lib" "%BinDir%\%Name%%Append%.so"
		move /y "%Name%.lib" "%LibDir%\%Name%.so"
	) else (
		set exitcode=1
		call :color error
		call :title error
		pause
	)
) else (
	set exitcode=1
	call :color error
	call :title error
	pause
)
:build_target

call :clean

exit /b %exitcode%



:color
goto color_%~1%~2
:color_build
color 17
goto color_
:color_buildcomplete
color 37
goto color_
:color_builddebug
color 57
goto color_
:color_buildtask
color 27
goto color_
:color_error
color 47
goto color_
:color_clean
color 87

:color_
exit /b %errorlevel%



:display
goto display_%~1
:display_compile
echo ^>^> Compiling sources...
goto display_
:display_linkelf
echo ^>^> Linking elf...
goto display_
:display_linklib
echo ^>^> Linking dynamic library...
goto display_
:display_postlink
echo ^>^> Postlinking...

:display_
exit /b %errorlevel%



:title
goto title_%~1%~2
:title_build
title Building "%Name%" - Standard
goto title_
:title_buildcomplete
title Building "%Name%" - Complete
goto title_
:title_builddebug
title Building "%Name%" - Debug
goto title_
:title_buildtask
title Building "%Name%" - Task
goto title_
:title_error
title Errors in "%Name%"
goto title_
:title_clean
title Cleaning "%Name%"

:title_
exit /b %errorlevel%



:compile
set _src=%~1
call :display compile
%gcc% "%_src:;=" "%" %C_Opts% %C_Opts_M680x0% -I"%IncludesDirs:;=" -I"%" -D%Defines: = -D%
exit /b %errorlevel%



:link
set _obj=%~2

call :display link%~1
goto link_target%~1

:link_targetelf
%ld% "%_obj:;=" "%" -pie %Ld_Opts% -L"%LibsDirs:;=" -L"%" -l:"%Libs:;=" -l:"%" -o"%~3"
goto link_target
:link_targetlib
%ld% "%_obj:;=" "%" -shared -soname=%Name%.so %Ld_Opts% -L"%LibsDirs:;=" -L"%" -l:"%Libs:;=" -l:"%" -o"%~3"
goto link_target
:link_target

exit /b %errorlevel%



:postlink
call :display postlink

goto postlink_target%~1

:postlink_targetelf
%pl% "%~2" -o "%~3"
goto postlink_target
:postlink_targetlib
%pl% -shared -def "%SrcDir%\%Name%_export.txt" "%~2" -o "%~3"
goto postlink_target
:postlink_target

exit /b %errorlevel%



:archive
call :configure

goto archive_%~1

7z a -t7z -mx9 -r "%Name%_src.7z" "%SrcDir%\*.*"

goto archive_%~1

:archive_src
7z a -t7z -mx9 -xr!*svn* "%SrcDir%\archive\%Name%_%now%.7z" ".\%SrcDir%\*.*"

goto archive_
:archive_all
7z a -t7z -mx9 -xr!*svn* "src.7z" ".\%SrcDir%\*.*"
7z a -tzip -mx9 -xr!*svn* "%BinDir%\archive\%Name%_%now%.zip" ".\%BinDir%\*.*" ".\%DocDir%\*" ".\%ResDir%\*" ".\src.7z"

del /f /q "src.7z"

goto archive_
:archive_

exit /b %errorlevel%



:clean
call :color clean
call :title clean
goto clean_%~1

:clean_
del /f /q "*.o" "*.pe" "*.lib"
goto clean_exit

:clean_all
del /f /q "*.o" "*.pe" "*.lib"
del /f /q "%BinDir%\*.*"
del /f /q "%LibDir%\*.*"
goto clean_exit

:clean_bin
del /f /q "%BinDir%\*.*"
del /f /q "%LibDir%\*.*"
goto clean_exit

:clean_exit
exit /b %errorlevel%



:get_name
set %2=%~n1
exit /b %errorlevel%



:init_dirs
if not exist %SrcDir% md %SrcDir%
if not exist %SrcDir%\archive md %SrcDir%\archive
if not exist %BinDir% md %BinDir%
if not exist %BinDir%\archive md %BinDir%\archive
if not exist %DocDir% md %DocDir%
if not exist %DocDir%\readme.txt echo Project: %Name%>%DocDir%\readme.txt
if not exist %LibDir% md %LibDir%
if not exist %ResDir% md %ResDir%
if not exist %EmuDir% md %EmuDir%
exit /b %errorlevel%